package com.sdp.irrigation.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.irrigation.db.irrigation_about.service.IIrrigationAboutService;
import com.sdp.irrigation.entity.IrrigationAbout;

/**
 * <p>
 * 关于我们 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2025-02-15
 */
@RestController
@RequestMapping("/IrrigationAboutController")
public class IrrigationAboutController {
	
	@Autowired
	private IIrrigationAboutService iIrrigationAboutService;
	
	@Autowired
	private Query query;

	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iIrrigationAboutService, IrrigationAbout.class, dataCenter));
	}
	

	@GetMapping(value="/queryAbout")
	public Result<?> queryAbout() {
		LambdaQueryWrapper<IrrigationAbout> queryWrapper = new LambdaQueryWrapper<>();
		IrrigationAbout about = iIrrigationAboutService.getOne(queryWrapper,false);
		if(about==null) {
			about= new IrrigationAbout();
		}
		return Result.ok(about);
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody IrrigationAbout about) {
		if(about==null) {
			return Result.error("关于我们信息为空");
		}
		about.setUpdateTime(LocalDateTime.now());
		if(StringUtils.isEmpty(about.getKeyId())) {
			about.setCreateTime(LocalDateTime.now());
			iIrrigationAboutService.save(about);
		}else {
			iIrrigationAboutService.updateById(about);
		}
		
		return Result.ok(about);
	}
}

